import Request from '@utils/request';

import { domain } from '@/utils';

const SLURMPATH = `http://slurm.${domain}`;
export const DIRSCROLL = `http://files.${domain}`;

// Retrieve the list of partitions in the current Slurm platform
export const getPartitions = (params = {}) => Request.get(`${SLURMPATH}/api/v1/slurm/partitions`, { params });
// Retrieve the number of compute nodes in the cluster
export const getNodes = (params = {}) => Request.get(
  `${SLURMPATH}/api/v1/slurm/${window.PROJECT_CONFIG.SCOPE_ID_NAME}/nodes/count`,
  { params },
);
// Retrieve total and free CPUs from Slurm
export const getCpuNums = (params = {}) => Request.get(
  `${SLURMPATH}/api/v1/slurm/${window.PROJECT_CONFIG.SCOPE_ID_NAME}/cpus`,
  { params },
);
// Get the list of GPU types supported by the current Slurm platform
export const getGpuNums = (params = {}) => {
  // return Request.get(`${SLURMPATH}/api/v1/slurm/gpu/types`, { params });
};
// Get the list of GPU types supported by the current Slurm platform
export const getGpuTypes = (params = {}) => Request.get(`${SLURMPATH}/api/v1/slurm/gpu/types`, { params });
// 根据作业账户获取作业列表，支持分页功能
// 全部
export const getJobAllList = (params = {}) => {
  const { pageSize, current: page, orderBy, userName, orgName } = params;
  return Request.get(`${SLURMPATH}/api/v1/slurm/db/jobs/all`, {
    params: { pageSize, page, orderBy, userName, orgName },
  });
};
// 根据作业 ID 获取作业列表
export const getJobById = (params = {}) => {
  const { pageSize, current: page, orderBy, id, userName, orgName } = params;
  return Request.get(`${SLURMPATH}/api/v1/slurm/db/jobs/job_id/${id}`, {
    params: { pageSize, page, orderBy, id, userName, orgName },
  });
};
// 根据作业迷城 获取作业列表
export const getJobByJobName = (params = {}) => {
  const {
    pageSize,
    current: page,
    orderBy,
    job_name,
    userName,
    orgName,
  } = params;
  return Request.get(`${SLURMPATH}/api/v1/slurm/db/jobs/name/:name`, {
    params: { pageSize, page, orderBy, job_name, userName, orgName },
  });
};
export const getAccountJobList = (params = {}) => {
  const {
    account,
    pageSize,
    current: page,
    orderBy,
    userName,
    orgName,
  } = params;
  return Request.get(`${SLURMPATH}/api/v1/slurm/db/jobs/account/${account}`, {
    params: { pageSize, page, orderBy, userName, orgName },
  });
};
export const getAccountJobListBystate = (params = {}) => {
  const {
    account,
    pageSize,
    current: page,
    state,
    orderBy,
    userName,
    orgName,
  } = params;
  return Request.get(
    `${SLURMPATH}/api/v1/slurm/db/jobs/account/${account}/state/${state}`,
    {
      params: { pageSize, page, orderBy, userName, orgName },
    },
  );
};
// Cancel a job that is currently running in the Slurm queue
export const handlerCancelJob = (params = {}) => Request.post(`${SLURMPATH}/api/v1/slurm/job/cancel`, { params });

//  1 获取 Slurm 服务器对应的 IP 接口
export const getSlurmHostIp = (params = {}) => Request.get(`${SLURMPATH}/api/v1/slurm/host/ip`, { params });

// 2 获取 Slurm 服务器对应的 AgentID 接口
export const getWhiteIP = ipStr => Request.get(`/job-manage/web/whiteIP/list?ipStr=${ipStr}&pageSize=99&start=0&current=1`);

// 3 基于 AgentID 接口获取服务器详情信息(hostId 由第二个接口获取
export const getHostsDetails = (params = {}) => Request.post(
  `/job-manage/web/scope/biz/${window.PROJECT_CONFIG.SCOPE_ID}/hosts/details`,
  { params },
);


export default {
  getNodes,
  getCpuNums,
  getWhiteIP,
  getJobById,
  getGpuTypes,
  getJobAllList,
  getPartitions,
  getHostsDetails,
  getSlurmHostIp,
  getJobByJobName,
  handlerCancelJob,
  getAccountJobList,
  getAccountJobListBystate,
};
